package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.clqteam.dao.removeShsDao;
import com.clqteam.util.JDBCutil;

public class removeShsDaoImpl implements removeShsDao{

	@Override
	public int removeShsByPid(int Pid) {
		int m = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" DELETE FROM s_product");
		sql.append(" WHERE s_product.pId =?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, Pid);
			int rst = pst.executeUpdate();
			if (rst==1) {
				m = rst;
			}else {
				m=0;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return m;
	}

	@Override
	public void setdirInfo(String drInfo,String drTime,int eId,String eName) {
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO s_dailyreport");
		sql.append(" (drTime,drInfo,eId,eName) VALUES (?,?,?,?)");
		Connection con  = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, drTime);
			pst.setString(2, drInfo);
			pst.setInt(3, eId);
			pst.setString(4, eName);
			pst.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public String getProductInfoByPid(int Pid) {
		String s = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM s_product");
		sql.append(" WHERE s_product.pId=?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, Pid);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				int i1 = rst.getInt("pId");
				String s1 = Integer.toString(i1);
				String s2 = rst.getString("pName");
				String s3 = rst.getString("pType");
				int i4 = rst.getInt("pAmount");
				String s4 = Integer.toString(i4);
				String s5 = rst.getString("pClass");
				double d6=rst.getDouble("pCost");
				String s6 = Double.toString(d6);
				double d7=rst.getDouble("pPrice");
				String s7 = Double.toString(d7);
				String s8 = rst.getString("pTime");
				int i9 = rst.getInt("supId");
				String s9 = Integer.toString(i9);
				StringBuffer ss = new StringBuffer();
				ss.append(s1);
				ss.append(";");
				ss.append(s2);
				ss.append(";");
				ss.append(s3);
				ss.append(";");
				ss.append(s4);
				ss.append(";");
				ss.append(s5);
				ss.append(";");
				ss.append(s6);
				ss.append(";");
				ss.append(s7);
				ss.append(";");
				ss.append(s8);
				ss.append(";");
				ss.append(s9);
				s =ss.toString();
			}
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return s;
	}

	@Override
	public void removeOrder(int oId) {
		StringBuffer sql = new StringBuffer();
		sql.append(" DELETE FROM s_order WHERE oId = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, oId);
			pst.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public int getOidByPid(int pId) {
		int m = 0;
		StringBuffer sql = new StringBuffer();
		 sql.append(" SELECT s_orderinfo.oId FROM s_orderinfo");
		 sql.append(" WHERE s_orderinfo.pId=?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, pId);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				m=rst.getInt("oId");
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return m;
	}

	@Override
	public void removeOrderInfo(int oId) {
		StringBuffer sql = new StringBuffer();
		sql.append(" DELETE FROM s_orderinfo WHERE oId = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, oId);
			pst.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void removeAccountInfo(int oId) {
		StringBuffer sql = new StringBuffer();
		sql.append(" DELETE FROM s_accountant WHERE oId = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, oId);
			pst.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	

}
